package io.geekidea.boot.test.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.test.dto.PayRecordDto;
import io.geekidea.boot.test.entity.PayRecord;
import io.geekidea.boot.test.mapper.PayRecordMapper;
import io.geekidea.boot.test.query.PayRecordQuery;
import io.geekidea.boot.test.service.PayRecordService;
import io.geekidea.boot.test.vo.PayRecordVo;
import io.geekidea.boot.test.query.AppPayRecordQuery;
import io.geekidea.boot.test.vo.AppPayRecordVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * 消费记录 服务实现类
 *
 * @author geekidea
 * @since 2025-03-26
 */
@Slf4j
@Service
public class PayRecordServiceImpl extends ServiceImpl<PayRecordMapper, PayRecord> implements PayRecordService {

    @Autowired
    private PayRecordMapper payRecordMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addPayRecord(PayRecordDto dto) {
        PayRecord payRecord = new PayRecord();
        BeanUtils.copyProperties(dto, payRecord);
        return save(payRecord);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updatePayRecord(PayRecordDto dto) {
        Long id = dto.getId();
        PayRecord payRecord = getById(id);
        if (payRecord == null) {
            throw new BusinessException("消费记录不存在");
        }
        BeanUtils.copyProperties(dto, payRecord);
        return updateById(payRecord);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deletePayRecord(Long id) {
        return removeById(id);
    }

    @Override
    public PayRecordVo getPayRecordById(Long id) {
        return payRecordMapper.getPayRecordById(id);
    }

    @Override
    public Paging<PayRecordVo> getPayRecordPage(PayRecordQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<PayRecordVo> list = payRecordMapper.getPayRecordPage(query);
        Paging<PayRecordVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppPayRecordVo getAppPayRecordById(Long id) {
        return payRecordMapper.getAppPayRecordById(id);
    }

    @Override
    public Paging<AppPayRecordVo> getAppPayRecordPage(AppPayRecordQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppPayRecordVo> list = payRecordMapper.getAppPayRecordPage(query);
        Paging<AppPayRecordVo> paging = new Paging<>(list);
        return paging;
    }

}
